using System;
using UnityEditor;
using UnityEngine;

namespace Runtime.Module.GameGraphic.Editor
{
    public class IsScriptCtrlEditorData<T> where T : Component
    {
        public IsScriptCtrlEditorData(SerializedObject serializedObject, T target)
        {
            this.serializedObject = serializedObject;
            this.target = target;
            try
            {
                InitData();
                SetScriptSet();
            }
            catch (Exception e)
            {
                string name = "";
                if (this.target)
                {
                    name = this.target.gameObject.name;
                }

                Debug.LogError($"{name}初始化脚本属性失败，err：{e.Message}");
            }
        }

        enum InfoCtrlType
        {
            Node,
            Component,
        }

        private InfoCtrlType CurrentInfoCtrlType = InfoCtrlType.Node;

        public string NodeInfoCtrlStr = "_NodeInfoCtrl";
        public string ComponentInfoCtrlStr = "_ComponentInfoCtrl";
        protected GameScriptSet<T> CurGameScriptSet { get; set; }
        protected GameSceneScriptSet<T> CurGameSceneScriptSet { get; set; }
        internal SerializedObject serializedObject;
        protected T target;
        public virtual string NodeInfoCtrlName { get; set; }

        public void SetScriptSet()
        {
            CurGameScriptSet = SetScriptStute<GameScriptSet<T>>();
            CurGameSceneScriptSet = SetScriptStute<GameSceneScriptSet<T>>();
        }

        private void InitData()
        {
            if (target is MonoBehaviour)
            {
                CurrentInfoCtrlType = InfoCtrlType.Component;
                NodeInfoCtrlName = ComponentInfoCtrlStr;
            }
            else
            {
                CurrentInfoCtrlType = InfoCtrlType.Node;
                NodeInfoCtrlName = NodeInfoCtrlStr;
            }
        }

        private TS SetScriptStute<TS>() where TS : GameScriptSet<T>, new()
        {
            TS temp = new TS()
            {
                serializedObject = serializedObject,
                NodeInfoCtrlName = NodeInfoCtrlName,
                target = target,
            };
            temp.GetScriptStute();
            return temp;
        }

        public virtual void OnInspectorGUIOfScript()
        {
            OnInspectorGUIOfComponentScript();
            OnInspectorGUIOfSceneScript();
        }

        public virtual void OnInspectorGUIOfComponentScript()
        {
            if (CurGameScriptSet != null)
            {
                if (CurGameScriptSet.IsGUI)
                {
                    EditorGUILayout.Space(10);
                    EditorGUI.BeginChangeCheck();
                    CurGameScriptSet.IsScript = EditorGUILayout.Toggle("IsScript", CurGameScriptSet.IsScript);
                    if (EditorGUI.EndChangeCheck())
                    {
                        CurGameScriptSet.SetScriptStute();
                    }

                    //EditorGUI.indentLevel++;
                    EditorGUILayout.Space(10);
                }
            }
        }

        public virtual void OnInspectorGUIOfSceneScript()
        {
            if (CurGameSceneScriptSet != null)
            {
                if (CurGameSceneScriptSet.IsGUI)
                {
                    EditorGUILayout.Space(10);
                    EditorGUI.BeginChangeCheck();
                    CurGameSceneScriptSet.IsScript =
                        EditorGUILayout.Toggle("IsSceneScript", CurGameSceneScriptSet.IsScript);
                    if (EditorGUI.EndChangeCheck())
                    {
                        CurGameSceneScriptSet.SetScriptStute();
                    }

                    EditorGUILayout.Space(10);
                }
            }
        }
    }
}